package com.example.managementjava.Student;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet("/DataFile")
public class DataFile extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("tapplication/x-dcx;charset=utf-8");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd-HHmmss");
        String timeStamp = simpleDateFormat.format(new Date());
        // 文件路径
        String path = request.getSession().getServletContext().getRealPath("/") + "\\" + "Download" + "\\" +"计算机学院请假表总汇--"+ timeStamp + ".docx";
        //  文件名
        String path2 = "\\" + "Download" + "\\" +"计算机学院请假表总汇--"+ timeStamp + ".docx";
        //  解决中文路径编码异常
        path2 = java.net.URLEncoder.encode(path2, "UTF-8").replace("+", "%20");
        response.setHeader("Content-disposition", "attachment; filename=\"" + path2 + "\"");

        PrintWriter out = response.getWriter();
        List<Student> list = Servlet_Stu.Stulist;
        out.write("总数："+list.size()+"\n");
        for (Student i : list){
            out.write(i.toString()+"\n");
        }
        System.out.println(timeStamp+"--Download");
//        out.write(path);

    }
}
